<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Log;
use Carbon\Carbon;
class LogController extends Controller
{
    public function getAll() {
        $log=Log::orderBy('id','desc')->get();
        return response()->json($log);
    }
    public function getCurrent($page) {
        $pages=$page*10-10;
        $log=Log::skip($pages)->take(10)->get();
        return response()->json($log);
    }
    public function one($id) {
        $log=Log::find($id);
        return response()->json($log);
    }

    public function getCount() {
        $log=Log::all()->count();
        return response()->json($log);
    }
    public function add(Request $request) {
        $add=$request->except('_token');

        $add['date']=date('Y-m-d',strtotime(Carbon::now()));
       Log::create($add);
        return response()->json('添加成功');
    }
    public function del ($id) {
        $log=Log::find($id);
        if($log){
            $log->delete();
            return response()->json('删除成功');
        }
        return response()->json('不存在');

    }
    public function update($id, Request $request) {
        $add=$request->except('_token');
        Log::where('id',$id)->update($add);
        return response()->json('修改成功');
    }
    public function search($word) {
        $log=Log::where('name','like','%'.$word.'%')->OrWhere('email','like','%'.$word.'%')->get();
        return response()->json($log);
    }
    public function page($page,$pagesize) {
        $skip=$page*$pagesize;
        $take=$pagesize;
        $log=Log::orderBy('id','desc')->skip($skip)->take($take)->get();
        return response()->json($log);
    }
    public function dateOne($date) {

        $log=Log::where('created_at',$date)->get();
        return response()->json($log);
    }
    public function datemore($date) {

        $log=Log::where('created_at',$date)->get();
        return response()->json($log);
    }

}
